package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

import java.io.IOException;

/**
 * GetResponseNormalList类
 * 实现获取响应列表的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class GetResponseNormalList implements AxdrType {

    private byte[] dataCode = null;
    private PIID piid_acd = null;
    private SubSeqOfResults results = null;

    /**
     * 默认构造函数
     */
    public GetResponseNormalList() {
    }

    /**
     * 设置数据编码
     * 
     * @param dataCode 数据编码
     */
    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    /**
     * 设置所有参数
     * 
     * @param piid_acd PIID
     * @param results  结果序列
     */
    public void setAll(PIID piid_acd, SubSeqOfResults results) {
        this.piid_acd = piid_acd;
        this.results = results;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;

        this.piid_acd = new PIID();
        this.piid_acd.setNumBit(8);
        codeLength += this.piid_acd.decode(input);

        this.results = new SubSeqOfResults();
        codeLength += this.results.decode(input);

        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        int codeLength;

        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                try {
                    output.write(this.dataCode[i]);
                } catch (java.io.IOException e) {
                    throw new RuntimeException(e);
                }
            }
        } else {
            if (this.piid_acd != null && this.results != null) {
                codeLength = 0;
                codeLength += this.results.encode(output);
                codeLength += this.piid_acd.encode(output);
            } else {
                throw new RuntimeException("results or piid_acd is null");
            }
        }
        return codeLength;
    }

    /**
     * 编码并保存
     * 
     * @param encodingSizeGuess 编码大小预估
     */
    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            encode(revOStream);
        } catch (RuntimeException e) {
            throw new RuntimeException("Error encoding GetResponseNormalList", e);
        }
        this.dataCode = revOStream.getArray();
    }

    @Override
    public String toString() {
        if (this.piid_acd != null && this.results != null) {
            return "sequence: {" + "piid_acd: " + this.piid_acd + ", results: " + this.results + "}";
        } else {
            return "piid_acd || results is null";
        }
    }

    // 添加getter方法
    public PIID getPiidAcd() {
        return this.piid_acd;
    }

    public SubSeqOfResults getResults() {
        return this.results;
    }
}